OPC Studio User's Guide and Reference
OPC Wizard Operation Monitoring Services
Concepts > OPC Wizard Concepts > OPC Wizard Features > OPC Wizard Component Services > OPC Wizard Operation Monitoring Services
In This Topic

Introduction

The operation monitoring services in OPC Wizard allow your code to receive information about the status of OPC UA server operations, such as whether the server endpoints have been successfully opened, and how and when are OPC UA clients connecting and disconnecting from the server. The operation monitoring services are purely for informational purposes, diagnostics and troubleshooting; you absolutely do not have to use these services if you do not want to.

IEasyUAServerEndpointMonitoring Service

In order for OPC UA server to be of any use in the OPC UA system, it needs to make its endpoints available ("open" them) for OPC UA client connections. The OPC Wizard servers implemented with EasyUAServer Class have one or more configurable endpoints. Depending on the transport protocol, "opening" the endpoint might mean different things. In case of TCP endpoints, opening an OPC UA endpoint requires creating a TCP socket that listens for incoming connection attempts.

When the Start Method on the EasyUAServer Class is called, the OPC Wizard tries to open all its configured endpoints, and keep them open until the Stop Method is called. For various reasons, an attempt to open the endpoint may fail. In case of TCP sockets, this is typically because some other application on the system is already using the socket with the same port number on the same network interface (it may be another OPC UA server misconfigured to use the same port, or even another instance of the same OPC UA server, running by mistake). If opening any of the server endpoints fails, the OPC Wizard will continue running, and will repeatedly try to re-open the endpoint.

The status of the OPC UA server endpoints is reported through the IEasyUAServerEndpointMonitoring service. Due to its importance, the IEasyUAServerEndpointMonitoring Interface is also implemented directly on the EasyUAServer Class, and you can therefore directly add event handlers for its events on the server object, without having to obtain the service first.

The EndpointStateChanged Event on this service interface is raised when the state of the OPC UA server endpoint has changed. You receive notifications when the endpoint is opening, when it is (successfully) open, when it is closing, and when it has closed (which may be due to a failure during opening). The event notification passes to you an instance of the EasyUAServerEndpointStateChangedEventArgs Class, which contains data about the condition of the server endpoint. The EndpointUrlString Property (and EndpointUrl Property) contain the URL of the endpoint, and the MessageSecurityModes Property contains its effective message security modes. The ConnectionState Property indicates the state of the server endpoint (it is open when the IsConnected Property is true). The Exception Property contains a null reference in case of success, or it contains an exception object in case of problems.

Note that the information provided by this event should be used for oversight, informational and diagnostics purposes only. Specifically, you should not use it to attempt to implement any error recovery mechanism, because error recovery is already built into the OPC Studio software, and having your own mechanism would inevitably cause conflicts.

.NET

// This example shows how to obtain notifications about the changes in the state of the endpoints that server exposes.
// You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-OPCStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.NodeSpace;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UAServerDocExamples._EasyUAServer
{
    class EndpointStateChanged
    {
        public static void Main1()
        {
            // Instantiate the server object.
            // By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            var server = new EasyUAServer();

            // Define a data variable providing random integers.
            var random = new Random();
            server.Add(new UADataVariable("MyDataVariable").ReadValueFunction(() => random.Next()));

            // Hook events.
            server.EndpointStateChanged += ServerOnEndpointStateChanged;

            // Start the server.
            Console.WriteLine("The server is starting...");
            server.Start();

            Console.WriteLine("The server is started.");
            Console.WriteLine();

            // Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...");
            Console.ReadLine();

            // Stop the server.
            Console.WriteLine("The server is stopping...");
            server.Stop();

            Console.WriteLine("The server is stopped.");
        }

        // Event handler for the EndpointStateChanged event. It simply prints out the event.
        private static void ServerOnEndpointStateChanged(object sender, EasyUAServerEndpointStateChangedEventArgs e)
        {
            Console.WriteLine(e);

            // Following are some useful properties in the event notification:
            //   e.EndpointUrlString
            //   e.ConnectionState
            //   e.Exception
            //   e.Succeeded
        }
    }
}
' This example shows how to obtain notifications about the changes in the state of the endpoints that server exposes.
' You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports System
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.NodeSpace
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace _EasyUAServer
    Partial Friend Class EndpointStateChanged
        Shared Sub Main1()
            ' Instantiate the server object.
            ' By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            Dim server = New EasyUAServer()

            ' Define a data variable providing random integers.
            Dim random = New Random()
            server.Add(New UADataVariable("MyDataVariable").ReadValueFunction(Function() random.Next()))

            ' Hook events.
            AddHandler server.EndpointStateChanged, AddressOf ServerOnEndpointStateChanged

            ' Start the server.
            Console.WriteLine("The server is starting...")
            server.Start()

            Console.WriteLine("The server is started.")
            Console.WriteLine()

            ' Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...")
            Console.ReadLine()

            ' Stop the server.
            Console.WriteLine("The server is stopping...")
            server.Stop()

            Console.WriteLine("The server is stopped.")
        End Sub

        ' Event handler for the EndpointStateChanged event. It simply prints out the event.
        Private Shared Sub ServerOnEndpointStateChanged(ByVal sender As Object, ByVal e As EasyUAServerEndpointStateChangedEventArgs)
            Console.WriteLine(e)

            ' Following are some useful properties in the event notification:
            '   e.EndpointUrlString
            '   e.ConnectionState
            '   e.Exception
            '   e.Succeeded
        End Sub
    End Class
End Namespace

IEasyUAServerConnectionMonitoring Service 

OPC UA clients connect to the OPC UA server endpoints, perform some operations, and then disconnect (a client may even be connected multiple times to the same OPC UA server at the same time). In order to monitor these OPC UA client connections on the server side, you can use the IEasyUAServerConnectionMonitoring service.

This service provide two main events that you can handle:

You can use these events for custom handling or logging of connections and disconnections. The service also provides the ClientSessionCount Property, which contains the number of client sessions that are currently connected to the server.

When the ClientSessionConnected Event or ClientSessionDisconnected Event is raised, your event handler receives an instance of the EasyUAClientSessionConnectionEventArgs Class as the event arguments. Following information is available to you from this class:

.NET

// This example shows how to monitor OPC UA client connections to the server.
// You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-OPCStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using Microsoft.Extensions.DependencyInjection;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.NodeSpace;
using OpcLabs.EasyOpc.UA.OperationModel;
using OpcLabs.EasyOpc.UA.Services;

namespace UAServerDocExamples._EasyUAServerConnectionMonitoring
{
    class ClientSessions
    {
        public static void Main1()
        {
            // Instantiate the server object.
            // By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            var server = new EasyUAServer();

            // Define a data variable providing random integers.
            var random = new Random();
            server.Add(new UADataVariable("MyDataVariable").ReadValueFunction(() => random.Next()));

            // Obtain the server connection monitoring service.
            IEasyUAServerConnectionMonitoring serverConnectionMonitoring = server.GetService<IEasyUAServerConnectionMonitoring>();
            if (serverConnectionMonitoring is null)
            {
                Console.WriteLine("The server connection monitoring service is not available.");
                return;
            }

            // Hook events.
            serverConnectionMonitoring.ClientSessionConnected += ServerConnectionMonitoringOnClientSessionConnected;
            serverConnectionMonitoring.ClientSessionDisconnected += ServerConnectionMonitoringOnClientSessionDisconnected;

            // Start the server.
            Console.WriteLine("The server is starting...");
            server.Start();

            Console.WriteLine("The server is started.");
            Console.WriteLine();

            // Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...");
            Console.ReadLine();

            // Stop the server.
            Console.WriteLine("The server is stopping...");
            server.Stop();

            Console.WriteLine("The server is stopped.");
        }

        // Event handler for the ClientSessionConnected event.
        static private void ServerConnectionMonitoringOnClientSessionConnected(
            object sender, 
            EasyUAClientSessionConnectionEventArgs e)

        {
            var serverConnectionMonitoring = (IEasyUAServerConnectionMonitoring)sender;
            Console.WriteLine(
                $"A client session has connected to endpoint \"{e.EndpointUrlString}\". " + 
                $"There is now {serverConnectionMonitoring.ClientSessionCount} client session(s).");
        }

        // Event handler for the ClientSessionDisconnected event.
        static private void ServerConnectionMonitoringOnClientSessionDisconnected(
            object sender,
            EasyUAClientSessionConnectionEventArgs e)
        {
            var serverConnectionMonitoring = (IEasyUAServerConnectionMonitoring)sender;
            Console.WriteLine(
                $"A client session has disconnected from endpoint \"{e.EndpointUrlString}\". " + 
                $"There is now {serverConnectionMonitoring.ClientSessionCount} client session(s).");
        }
    }
}
' This example shows how to monitor OPC UA client connections to the server.
' You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports System
Imports Microsoft.Extensions.DependencyInjection
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.NodeSpace
Imports OpcLabs.EasyOpc.UA.OperationModel
Imports OpcLabs.EasyOpc.UA.Services

Namespace _EasyUAServerConnectionMonitoring
    Partial Friend Class ClientSessions
        Shared Sub Main1()
            ' Instantiate the server object.
            ' By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            Dim server = New EasyUAServer()

            ' Define a data variable providing random integers.
            Dim random = New Random()
            server.Add(New UADataVariable("MyDataVariable").ReadValueFunction(Function() random.Next()))

            ' Obtain the server connection monitoring service.
            Dim serverConnectionMonitoring As IEasyUAServerConnectionMonitoring = server.GetService(Of IEasyUAServerConnectionMonitoring)
            If serverConnectionMonitoring Is Nothing Then
                Console.WriteLine("The server connection monitoring service is not available.")
                Exit Sub
            End If


            ' Hook events.
            AddHandler serverConnectionMonitoring.ClientSessionConnected, AddressOf ServerConnectionMonitoringOnClientSessionConnected
            AddHandler serverConnectionMonitoring.ClientSessionDisconnected, AddressOf ServerConnectionMonitoringOnClientSessionDisconnected

            ' Start the server.
            Console.WriteLine("The server is starting...")
            server.Start()

            Console.WriteLine("The server is started.")
            Console.WriteLine()

            ' Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...")
            Console.ReadLine()

            ' Stop the server.
            Console.WriteLine("The server is stopping...")
            server.Stop()

            Console.WriteLine("The server is stopped.")
        End Sub

        ' Event handler for the ClientSessionConnected event.
        Private Shared Sub ServerConnectionMonitoringOnClientSessionConnected(ByVal sender As Object, ByVal e As EasyUAClientSessionConnectionEventArgs)
            Dim serverConnectionMonitoring As IEasyUAServerConnectionMonitoring = sender
            Console.WriteLine(
                $"A client session has connected to endpoint ""{e.EndpointUrlString}"". " +
                $"There is now {serverConnectionMonitoring.ClientSessionCount} client session(s).")
        End Sub

        ' Event handler for the ClientSessionDisconnected event.
        Private Shared Sub ServerConnectionMonitoringOnClientSessionDisconnected(ByVal sender As Object, ByVal e As EasyUAClientSessionConnectionEventArgs)
            Dim serverConnectionMonitoring As IEasyUAServerConnectionMonitoring = sender
            Console.WriteLine(
                $"A client session has disconnected from endpoint ""{e.EndpointUrlString}"". " +
                $"There is now {serverConnectionMonitoring.ClientSessionCount} client session(s).")
        End Sub
    End Class
End Namespace
See Also

Reference